#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

const int N = 1010;

int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { 1, -1, 0, 0 };

int n, m;
int x1, y1, x2, y2;

char arr[N][N];
int dist[N][N];

int bfs()
{
    if (arr[x2][y2] == '*') return -1;

    memset(dist, -1, sizeof dist);
    queue<pair<int, int>> q;
    q.push({ x1, y1 });
    dist[x1][y1] = 0;

    while (q.size())
    {
        auto [a, b] = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int x = a + dx[i], y = b + dy[i];
            if (x >= 1 && x <= n && y >= 0 && y <= m && arr[x][y] == '.' && dist[x][y] == -1)
            {
                q.push({ x, y });
                dist[x][y] = dist[a][b] + 1;
                if (x == x2 && y == y2) return dist[x2][y2];
            }
        }
    }

    return -1;
}


int main()
{
    cin >> n >> m >> x1 >> y1 >> x2 >> y2;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> arr[i][j];
        }
    }

    cout << bfs() << endl;

    return 0;
}
